package com.kede.rest;

import com.kede.annotation.Log;
import com.kede.dictenum.BoolDictEnum;
import com.kede.domain.LocalStorage;
import com.kede.domain.QiniuContent;
import com.kede.domain.ResourceFile;
import com.kede.rest.po.ResourceAreaMapPo;
import com.kede.service.LocalStorageService;
import com.kede.service.QiNiuService;
import com.kede.service.ResourceFileService;
import com.kede.service.dto.ResourceFileDto;
import com.kede.service.dto.ResourceFileQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

/**
* @author Pr.li
* @date 2023-04-20
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "resource管理")
@RequestMapping("/api/resourceFile")
public class ResourceFileController {

    private final ResourceFileService resourceFileService;
    private final QiNiuService qiNiuService;
    private final LocalStorageService localStorageService;

    /**
     * 导出数据
     **/
    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('resourceFile:list')")
    public void exportResourceFile(HttpServletResponse response, ResourceFileQueryCriteria criteria) throws IOException {
        resourceFileService.download(resourceFileService.queryAll(criteria), response);
    }

    /**
    * 查询resource
    **/
    @GetMapping
    @Log("查询resource")
    @ApiOperation("查询resource")
    @PreAuthorize("@el.check('resourceFile:list')")
    public ResponseEntity<Object> queryResourceFile(ResourceFileQueryCriteria criteria, ResourceAreaMapPo resourceAreaMapPo, Pageable pageable){
        return new ResponseEntity<>(resourceFileService.queryAll(criteria,resourceAreaMapPo,pageable),HttpStatus.OK);
    }


    @Log("上传文件")
    @ApiOperation("上传文件")
    @PostMapping
    public ResponseEntity<Object> uploadQiNiu(@RequestParam MultipartFile file,@RequestParam("function") Integer function
    ,@RequestParam("islocal") Integer islocal,@RequestParam(value = "link",required = false,defaultValue = "") String link,@RequestParam("name") String name){
        ResourceFile resourceFile = new ResourceFile();
        resourceFile.setName(name);
        resourceFile.setFunction(function);
        resourceFile.setIslocal(islocal);
        resourceFile.setLink(link);
        ResourceFileDto resourceFileDto = resourceFileService.create(resourceFile,file);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }


    /**
    * 修改resource
    **/
    @PutMapping
    @Log("修改resource")
    @ApiOperation("修改resource")
    @PreAuthorize("@el.check('resourceFile:edit')")
    public ResponseEntity<Object> updateResourceFile(@Validated @RequestBody ResourceFile resources){
        resourceFileService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }


    /**
    * 删除resource
    **/
    @DeleteMapping
    @Log("删除resource")
    @ApiOperation("删除resource")
    @PreAuthorize("@el.check('resourceFile:del')")
    public ResponseEntity<Object> deleteResourceFile(@RequestBody Integer[] ids) {
        resourceFileService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}